<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <title>RequestBody传递JSON类型的参数</title>
    <script src="./js/jquery-3.4.1.min.js"></script>
    <script src="./js/bootstrap.js"></script>
    <link rel="stylesheet" href="./css/bootstrap.css" />
</head>
<body>
<button onclick="sendAjax();" class="btn btn-success">发送JSON数据到后端</button>

<script>
    function sendAjax() {
        let user = {id: 1, name: "", age: 18, salary: 11.2483};
        let userDate = JSON.stringify(user);
        // 使用ajax发送json数据到后端控制器
        $.ajax({
            url: "http://localhost:8080${pageContext.request.contextPath}/ajax/test1",
            type: "post",
            contentType: "application/json",
            data: userDate,  // 发送的json类型参数
            success:function (res) {  //请求成功后的回调函数
                // console.log 前端调试的时候，在浏览器控制器输出信息
                console.log(res);
            }
        });
    }
</script>
</body>
</html>